package com.gln.bloger.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.struts.action.ActionServlet;

import com.gln.bloger.log.Logging;

/**
 * Initial Servlet inherit from Struts ActionServlet
 * 
 * @author LeoLeung
 * Date: 2008/01/02
 * Update Date: 2008/01/05
 */
public final class BlogActionServlet extends ActionServlet {

	/**
	 *  serial version uid
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		this.initLog4j();
		super.init();
	}

	/**
	 * Initial for Log4j
	 * 
	 * @author LeoLeung
	 */
	private void initLog4j()
	{
		String filePath = this.getRootPath()+this.getInitParameter("log4j-init");
		
		if (null != filePath) 
		{
			DOMConfigurator.configure(filePath);
			Logging.info("Log4j Init Succeed.");
		}
		else
		{
			BasicConfigurator.configure();
			Logging.error("Log4j Init Failed.");
		}
	}
	
	/**
	 * Get real path of root
	 * 
	 * @author LeoLeung
	 */
	private String getRootPath()
	{
		return getServletContext().getRealPath("/");
	}
	
	/**
	 * Solve the encoding problem
	 */
	@Override
	protected void process(HttpServletRequest request,
	        HttpServletResponse response)
	        throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");	
		super.process(request, response);
	}
}
